﻿using Chromium;
using Chromium.Event;
using NetDimension.NanUI;
using NetDimension.NanUI.Browser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MealCheck
{
    public partial class Login : Formium
    {
        string name = "", pwd = "";

        public Login()
        {
            Title = "报餐系统登录";
        }

        public override string StartUrl => "http://www.egbt.net/html/login.html";

        public override HostWindowType WindowType => HostWindowType.Borderless;

        protected override Control LaunchScreen => null;

        protected override void OnRegisterGlobalObject(JSObject global)
        {
            var getName = global.AddFunction("getName");
            getName.Execute += Name_Execute;

            var getPwd = global.AddFunction("getPwd");
            getPwd.Execute += Pwd_Execute;

            var optLogin = global.AddFunction("optLogin");
            optLogin.Execute += Login_Execute;
        }

        private void Name_Execute(object sender, Chromium.Remote.Event.CfrV8HandlerExecuteEventArgs e)
        {
            name = ((e.Arguments.FirstOrDefault(p => p.IsString)?.StringValue) ?? string.Empty).Trim();
        }

        private void Pwd_Execute(object sender, Chromium.Remote.Event.CfrV8HandlerExecuteEventArgs e)
        {
            pwd = ((e.Arguments.FirstOrDefault(p => p.IsString)?.StringValue) ?? string.Empty).Trim();
        }

        private void Login_Execute(object sender, Chromium.Remote.Event.CfrV8HandlerExecuteEventArgs e)
        {
            //执行登录任务
            MessageBox.Show("用户名：" + name + "密码：" + pwd);
        }

        protected override void OnWindowReady(IWebBrowserHandler browserClient)
        {
            browserClient.LoadHandler.OnLoadEnd += LoadHandler_OnLoadEnd;
        }

        protected override void OnBorderlessFormStyle(IBorderlessHostWindowStyle style)
        {
            base.OnBorderlessFormStyle(style);
            style.WindowState = FormWindowState.Maximized;
            style.StartPosition = FormStartPosition.CenterScreen;
            style.Borders = new Padding(0);
        }

        private void LoadHandler_OnLoadEnd(object sender, CfxOnLoadEndEventArgs e)
        {
#if DEBUG
            CfxWindowInfo windowInfo = new CfxWindowInfo();

            windowInfo.Style = WindowStyle.WS_OVERLAPPEDWINDOW | WindowStyle.WS_CLIPCHILDREN | WindowStyle.WS_CLIPSIBLINGS | WindowStyle.WS_VISIBLE;
            windowInfo.ParentWindow = IntPtr.Zero;
            windowInfo.WindowName = "Dev Tools";
            windowInfo.X = 200;
            windowInfo.Y = 200;
            windowInfo.Width = 800;
            windowInfo.Height = 600;

            WebBrowser.BrowserHost.ShowDevTools(windowInfo, new CfxClient(), new CfxBrowserSettings(), null);
#endif
            // Check if it is the main frame when page has loaded.
            if (e.Frame.IsMain)
            {
                //页面加载完成时 执行
            }
        }
    }
}
